//
//  CCTestModel.m
//  Party
//
//  Created by its on 15/3/9.
//  Copyright (c) 2015年 coco. All rights reserved.
//

#import "CCPartyModel.h"
#import "AFHTTPRequestOperationManager+RACSupport.h"
#import <ReactiveMantle/RACSignal+Mantle.h>
#import "CCServer.h"

@implementation CCPartyModel

- (RACSignal *)requestOld {
  NSString *queryURL = nil;
  RACSignal *signal =
  [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    AFHTTPRequestOperationManager *manager =
    [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    RACDisposable *disposable = [[[[[manager rac_GET:queryURL parameters:nil] replayLazily]
                                   map:^id(RACTuple *value) {
                                     return value.first;
                                   }] parseResponseForClass:[CCPartyResultModel class]]
                                 subscribeNext:^(CCPartyResultModel *x) {
                                   if (x.error.code == 0) {
                                     [subscriber sendNext:x.dataArray];
                                     _footid = [(CCPartyResult*)x.dataArray[x.dataArray.count-1] activityID];
                                   } else {
                                     [subscriber sendError:x.error];
                                   }
                                 }
                                 error:^(NSError *error) {
                                   [subscriber sendError:error];
                                 }
                                 completed:^{
                                   [subscriber sendCompleted];
                                 }];
    RACCompoundDisposable *compoundDisposable =
    [RACCompoundDisposable compoundDisposable];
    [compoundDisposable addDisposable:disposable];
    return compoundDisposable;
  }];
  return signal;

}

- (RACSignal *)requestNew {
  NSString *queryURL = [NSString stringWithFormat:@"%@?start=%@&count=10",
                        CC_LIST_URL_HEAD,_headid];
  RACSignal *signal =
  [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    AFHTTPRequestOperationManager *manager =
    [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    RACDisposable *disposable = [[[[[manager rac_GET:queryURL parameters:nil] replayLazily]
                                   map:^id(RACTuple *value) {
                                     return value.first;
                                   }] parseResponseForClass:[CCPartyResultModel class]]
                                 subscribeNext:^(CCPartyResultModel *x) {
                                   if (x.error.code == 0) {
                                     [subscriber sendNext:x.dataArray];
                                     _headid = [(CCPartyResult*)x.dataArray[0] activityID];
                                   } else {
                                     [subscriber sendError:x.error];
                                   }
                                 }
                                 error:^(NSError *error) {
                                   [subscriber sendError:error];
                                 }
                                 completed:^{
                                   [subscriber sendCompleted];
                                 }];
    RACCompoundDisposable *compoundDisposable =
    [RACCompoundDisposable compoundDisposable];
    [compoundDisposable addDisposable:disposable];
    return compoundDisposable;
  }];
  return signal;

}

- (RACSignal *) requestAll {
  NSString *queryURL = nil;
  RACSignal *signal =
  [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    AFHTTPRequestOperationManager *manager =
    [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    RACDisposable *disposable = [[[[[manager rac_GET:queryURL parameters:nil] replayLazily]
                                   map:^id(RACTuple *value) {
                                     return value.first;
                                   }] parseResponseForClass:[CCPartyResultModel class]]
                                 subscribeNext:^(CCPartyResultModel *x) {
                                   if (x.error.code == 0) {
                                     [subscriber sendNext:x.dataArray];
                                     _footid = [(CCPartyResult*)x.dataArray[x.dataArray.count-1] activityID];
                                     _headid = [(CCPartyResult*)x.dataArray[0] activityID];
                                   } else {
                                     [subscriber sendError:x.error];
                                   }
                                 }
                                 error:^(NSError *error) {
                                   [subscriber sendError:error];
                                 }
                                 completed:^{
                                   [subscriber sendCompleted];
                                 }];
    RACCompoundDisposable *compoundDisposable =
    [RACCompoundDisposable compoundDisposable];
    [compoundDisposable addDisposable:disposable];
    return compoundDisposable;
  }];
  return signal;
  
}
@end


@implementation CCPartyResultModel

#pragma mark MTLJSONSerializing
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
  return @{
    @"hasMore" : @"hasmore",
    @"error" : @"error",
    @"dataArray" : @"data"
  };
}

+ (NSValueTransformer *) errorJSONTransformer {
  return [MTLValueTransformer reversibleTransformerWithBlock:^id(NSDictionary *error) {
    return [NSError errorWithDomain:@"error" code:0 userInfo:error];
  }];
}

+ (NSValueTransformer *)dataArrayJSONTransformer {
  return [NSValueTransformer mtl_JSONArrayTransformerWithModelClass:[CCPartyResult class]];
}
@end


@implementation CCPartyResult

#pragma mark MTLJSONSerializing
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
  return @{
    @"activityID" : @"id",
    @"coverImage" : @"coverImage",
    @"title" : @"title",
    @"money" : @"money",
    @"followCount" : @"collectCount",
    @"activityTime" : @"activityTime",
    @"applyCount" : @"applyCount",
    @"author" : @"author",
    @"address" : @"address"
  };
}

+ (NSValueTransformer *) authorJSONTransformer {
  return [MTLValueTransformer reversibleTransformerWithBlock:^id(NSDictionary *dic) {
    return [MTLJSONAdapter modelOfClass:[CCUserInfo class] fromJSONDictionary:dic error:NULL];
  }];
}
@end
